package com.yimi.yinhepda.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.bean.KuaYueTaskDetailEntity;
import com.yimi.yinhepda.utils.HScrollListView.HScrollView;

import java.util.List;

/**
 * 跨越点货列表适配器
 *
 * @author 000432
 */
public class KuaYueSpotGoodsAdapter extends BaseAdapter {
    LayoutInflater inflater;
    private Context context;
    private List<KuaYueTaskDetailEntity> dataList;
    private LinearLayout mListHead;

    public KuaYueSpotGoodsAdapter(Context context, List<KuaYueTaskDetailEntity> dataList, LinearLayout listHead) {
        this.context = context;
        this.dataList = dataList;
        this.mListHead = listHead;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return null != dataList ? dataList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        KuaYueTaskDetailEntity item = dataList.get(position);
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_kuayue_spot_goods, null);
            //横向滚动view
            holder.hScrollView = convertView.findViewById(R.id.horizontalScrollView1);
            // 运单号
            holder.waybillnum = convertView.findViewById(R.id.tv_waybillnum);
            // 货区
            holder.areaCode = convertView.findViewById(R.id.tv_areaCode);
            // 交接
            holder.activite = convertView.findViewById(R.id.tv_activite);
            // 已扫
            holder.scaned = convertView.findViewById(R.id.tv_scaned);
            // 差异
            holder.diff = convertView.findViewById(R.id.tv_diff);
            // 下一站
            holder.nextStation = convertView.findViewById(R.id.tv_nextStation);

            HScrollView headSrcrollView = mListHead.findViewById(R.id.horizontalScrollView1);
            headSrcrollView.AddOnScrollChangedListener(new OnScrollChangedListenerImp(holder.hScrollView));

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //运单号
        if (item.getOutsideNo() == null || "".equals(item.getOutsideNo())) {
            holder.waybillnum.setText(item.getWaybillNo());
        } else {
            holder.waybillnum.setText(item.getOutsideNo());
        }
        //货区
        holder.areaCode.setText(item.getAreaCode());
        //交接
        holder.activite.setText(String.format("%s", item.getPieceNum()));
        // 已扫
        holder.scaned.setText(String.format("%s", item.getScanCount()));
        // 差异
        holder.diff.setText(String.format("%s", Math.abs(item.getDifference())));
        //下一站
        holder.nextStation.setText(item.getNextDeptName());

        switch (item.getType()) {
            case 0:
                convertView.setBackgroundColor(context.getResources().getColor(R.color.red));
                break;
            case 1:
                convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
                break;
            case 2:
                convertView.setBackgroundColor(context.getResources().getColor(R.color.yellow));
                break;
            case 3:
                convertView.setBackgroundColor(context.getResources().getColor(R.color.green));
                break;
            default:
                break;
        }
        return convertView;
    }

    static class OnScrollChangedListenerImp implements HScrollView.OnScrollChangedListener {
        HScrollView mScrollViewArg;

        public OnScrollChangedListenerImp(HScrollView scrollViewar) {
            mScrollViewArg = scrollViewar;
        }

        @Override
        public void onScrollChanged(int l, int t, int oldl, int oldt) {
            mScrollViewArg.smoothScrollTo(l, t);
        }
    }

    static class ViewHolder {
        /**
         * 运单号
         */
        TextView waybillnum;
        /**
         * 库区编码
         */
        TextView areaCode;
        /**
         * 交接
         */
        TextView activite;
        /**
         * 扫描
         */
        TextView scaned;
        /**
         * 差异
         */
        TextView diff;
        /**
         * 下一站
         */
        TextView nextStation;
        /**
         * 横向滚动view
         */
        HScrollView hScrollView;
    }

    public void setDataList(List<KuaYueTaskDetailEntity> dataList) {
        this.dataList = dataList;
    }
}
